package com.example.json;

import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

public class StockCode 
{
	public static long getNextStockCode() 
	{
		Entity Stock = getStockCode("ID");
		
		if (Stock == null) 
		{
			Stock = new Entity("StockCode", "ID");
			Stock.setProperty("Code", 1);
			Util.persistEntity(Stock);			
			return 1;
		}
		else
		{
			return ((long)Stock.getProperty("Code"))+1;
		}
	}
	
	public static void putStockCode(long code) 
	{
		Entity Stock = getStockCode("ID");
		
		if (Stock != null) 
		{
			Stock.setProperty("Code", code);
			Util.persistEntity(Stock);
		}
	}

	public static Entity getStockCode(String name) 
	{
		Key key = KeyFactory.createKey("StockCode", name);
		return Util.findEntity(key);
	}
}
